#include<iostream>
#include<string.h>

using std::cout;
using std::endl;

class String
{
public:
    String()
        :_pstre(new char[10]())
        ,_pend(_pstre +10)
         ,_plast(_pstre+9)
    {
        cout<<"no param"<<endl;
    }
    String(const char *pstr);
    String(const String& rhs);
    String &operator=(const String &rhs);
    ~String();
    void print();
    size_t length() const;
    const char* c_str() const;
private:
    char *_pstre=nullptr;
    char *_pend=nullptr;
    char *_plast=nullptr;
};
String::String(const char* pstr)
    :_pstre(new char [strlen(pstr)+1]())
    ,_pend(_pstre+(strlen(pstr)+1)*sizeof(char))
     ,_plast(_pend-sizeof(char))
     //指针不能求和
{
    strcpy(_pstre,pstr);
    cout<<"init const char*"<<endl;
}

String::String(const String& rhs)
    :_pstre(new char[strlen(rhs._pstre)+1]())
    ,_pend(_pstre+(strlen(rhs._pstre)+1)*sizeof(char))
     ,_plast(_pend-sizeof(char))
{
    strcpy(_pstre,rhs._pstre);
    cout<<"copy constructors "<<endl;
}

String & String::operator=(const String &rhs)
{
    if(this!=&rhs)//自拷贝
    {
        delete []this->_pstre;
        _pstre=nullptr;
        _pend=nullptr;
        _plast=nullptr;
        this->_pstre=new char[strlen(rhs._pstre)+1];
        _pend=_pstre+(strlen(rhs._pstre)+1)*sizeof(char);
        _plast=_pend-sizeof(char);
        strcpy(_pstre,rhs._pstre);
    }
    cout<<"operator func"<<endl;
    return *this;
}

String::~String()
{
    if(_pstre!=nullptr)
    {
        delete []_pstre;
        _pstre=nullptr;
        _pend=nullptr;
        _plast=nullptr;
    }
    cout<<"~String"<<endl;
}

void String::print()
{
    cout<<"\""<<_pstre<<"\""<<endl;
}

size_t String::length() const
{
    size_t num;
    num=(_plast-_pstre)*sizeof(char);
    return num;
}

const char * String::c_str() const
{
    cout<<"data or c_str func"<<endl;
    return _pstre;
}

int main()
{
    String str1;
    str1.print();

    String str2 ="hello,world";
    String str3("wangdao");

    str2.print();
    str3.print();

    String str4 =str3;
    cout<<str4.length()<<endl;
    str4.print();

    return 0;
}

